package com.vouov.apps.service.impl;

import com.vouov.apps.bean.User;
import com.vouov.apps.mapper.UserMapper;
import com.vouov.apps.service.interfaces.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * User: yuminglong
 * Date: 11-10-27
 * Time: 上午2:17
 * Version: 1.0.0
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    @Transactional
    public void addUsers(User[] users) throws Exception {
        if (users != null) {
            for (User user : users) {
                userMapper.insertUser(user);
            }
        }
    }

    @Override
    public void addUser(User user) throws Exception {
        if (user != null) {
            userMapper.insertUser(user);
        }
    }

    @Override
    @Transactional
    public void removeUsers(User[] users) throws Exception {
        if (users != null) {
            for (User user : users) {
                userMapper.deleteUser(user);
            }
        }
    }

    @Override
    public void removeUser(User user) throws Exception {
        if (user != null) {
            userMapper.deleteUser(user);
        }
    }

    @Override
    @Transactional
    public void updateUsers(User[] users) throws Exception {
        if (users != null) {
            for (User user : users) {
                userMapper.updateUser(user);
            }
        }
    }

    @Override
    public void updateUsers(User user) throws Exception {
        if (user != null) {
            userMapper.updateUser(user);
        }
    }

    @Override
    public User getUserById(long id) throws Exception {
        return userMapper.getUserById(id);
    }

    @Override
    public List<User> queryUsers(String userName, int sex, int limitStart, int limit) {
        return userMapper.queryUsers(userName, sex, limitStart, limit);
    }

    @Override
    public int queryUsersCount(String userName, int sex) {
        return userMapper.queryUsersCount(userName, sex);
    }
}
